package com.sz.rexiang.Activity;

import java.nio.channels.SelectableChannel;

import org.xutils.view.annotation.ContentView;

import com.sz.rexiang.R;
import com.sz.rexiang.Fragment.FragmentLt;
import com.sz.rexiang.Fragment.FragmentMsgCenter;
import com.sz.rexiang.Fragment.FragmentMy;
import com.sz.rexiang.Fragment.FragmentSh;
import com.sz.rexiang.Fragment.FragmentShAd;

import android.app.Activity;
import android.media.tv.TvTrackInfo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;


@ContentView(R.layout.activity_main)
public class MainActivity extends BaseFgActivity implements OnClickListener {
	private TextView titleTv, currentPressedTv;
	private FragmentSh fgSh;
	private FragmentMsgCenter fgMsgCenter;
	private FragmentMy fgMy;
	private FragmentLt fgLt;
	private Fragment currFg;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
		initData();
		setListener();

	}

	private void setListener() {
		// TODO Auto-generated method stub
		this.findViewById(R.id.activity_main_tv_sh).setOnClickListener(this);
		this.findViewById(R.id.activity_main_tv_msgcenter).setOnClickListener(this);
		this.findViewById(R.id.activity_main_tv_my).setOnClickListener(this);
		this.findViewById(R.id.activity_main_tv_lt).setOnClickListener(this);

	}

	private void initView() {
		titleTv = (TextView) findViewById(R.id.ac_main_titletv);
	}

	public void setTitleTv(int resID) {

		titleTv.setText(resID);

	}

	private void initData() {
		fgMsgCenter = new FragmentMsgCenter();
		fgMy = new FragmentMy();
		fgSh = new FragmentSh();
		fgLt = new FragmentLt();
		showFg(fgMy, this.findViewById(R.id.activity_main_tv_my), R.string.ac_main_my);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

		switch (v.getId()) {
		case R.id.activity_main_tv_sh:

			showFg(fgSh, v, R.string.ac_main_sh);

			break;
		case R.id.activity_main_tv_msgcenter:
			showFg(fgMsgCenter, v, R.string.ac_main_msgcenter);

			break;
		case R.id.activity_main_tv_my:

			showFg(fgMy, v, R.string.ac_main_my);

			break;
		case R.id.activity_main_tv_lt:

			showFg(fgLt, v, R.string.ac_main_lt);

			break;

		default:
			break;
		}

	}

	/**
	 * ��ʾFg,���õ��Tv��״̬
	 * 
	 * @param fg
	 * @param v
	 * @param resID
	 */
	private void showFg(Fragment fg, View v, int resID) {
		// TODO Auto-generated method stub
		tration = manager.beginTransaction();
		// �ָ�δѡ��״̬
		if (currentPressedTv != null)
			currentPressedTv.setSelected(false);
		if (currFg != null)
			tration.hide(currFg);

		if (fg.isAdded()) {
			tration.show(fg);
		} else {
			tration.add(R.id.main_container, fg);
		}
		currentPressedTv = (TextView) v;
		// ����ѡ��״̬
		v.setSelected(true);
		// ���ñ���
		titleTv.setText(resID);
		tration.commit();
		currFg = fg;
	}

}
